package com.onpositive.units;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.onpositive.knowledge.model.values.IHasValue;

public class ParsedFileList extends ParsedValue {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1825360136710192539L;

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((fileList == null) ? 0 : fileList.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ParsedFileList other = (ParsedFileList) obj;
		if (fileList == null) {
			if (other.fileList != null)
				return false;
		} else if (!fileList.equals(other.fileList))
			return false;
		return true;
	}

	static public final String ID = "file-list" ;
	
	ArrayList<String> fileList = new ArrayList<String>() ;
	
	public ParsedFileList( ArrayList<String> value, String comment) {
		super(comment) ;
		this.fileList=value;
	}
	
	private ParsedFileList( ParsedFileList other, ArrayList<String> value, String comment )
	{
		super( comment ) ;		
		this.fileList = value ;		
	}
	
	public ParsedFileList( ParsedValue pv, String comment )
	{
		super( comment ) ;		
		ParsedFileList other = (ParsedFileList) pv ;
		this.fileList = new ArrayList<String>( other.fileList );		
	}
	
	public ParsedFileList( Element element )
	{
		super(element) ;
		int size = 0 ;	
		try{
			size = Integer.parseInt( element.getAttribute("size")) ;
		}catch(NumberFormatException e){
			return ;
		}
		if(size == 0 )
			return ;
		
		fileList = new ArrayList<String>();
		
		for( int i = 0 ; i < size ; i++ )
			fileList.add( element.getAttribute( "file"+i ).trim() ) ;
	}

	@Override
	public Object value() {
		return fileList;
	}

	@Override
	public Object describer() {
		return String.class;
	}

	@Override
	public void writeValue(Element element)
	{
		super.writeValuePartial(element) ;
		
		if( fileList == null )
			return ;
		
		int s = fileList.size() ;
		element.setAttribute("size", ""+s ) ;
		for( int i = 0 ; i < s ; i++ )
			element.setAttribute( "file"+i, fileList.get(i) != null ? fileList.get(i) : "" ) ;			
	}

	@Override
	public String getId() {
		return ID;
	}

	@Override
	public String getValueAsString() {
		
		String result = "" ;
		if( fileList != null && fileList.size() > 0 )
		{
			result += fileList.get(0) ;
			for( int i = 1 ; i < fileList.size() ; i++ )
				result += ("; "+fileList.get(i)) ;			
		}
		return result;
	}
	
	public ArrayList<String> getValuesList()
	{
		ArrayList<String> result = null ;
		if( fileList != null && fileList.size() > 0 )
		{
			result = new ArrayList<String>() ;
			result.add( fileList.get(0) );
			for( int i = 1 ; i < fileList.size() ; i++ )
				result.add( fileList.get(i) ) ;			
		}
		return result;
	}

	@Override
	public void setValue(Object obj) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public IHasValue clone(Object value, String comment) {
		if( !(value instanceof ArrayList<?> ))
			return null ;
		if( comment == null )
			comment = this.getComment() ;
		return new ParsedFileList( this, (ArrayList<String>)value, comment ) ;
	}

	@Override
	public <T> Collection<T> getValues(Class<T> vs) {
		return Collections.emptySet();
	}

}
